﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;


namespace QuanLyKhoThucPham
{
    public partial class usNhapKho : UserControl
    {
        public usNhapKho()
        {
            InitializeComponent();
        }

        public void LoadHienThi()
        {
            ng.Text = System.DateTime.Now.Day.ToString();
            th.Text = System.DateTime.Now.Month.ToString();
            nm.Text = System.DateTime.Now.Year.ToString();
        }


        private void usNhapKho_Load(object sender, EventArgs e)
        {
            LoadHienThi();
            LoadNhaPhanPhoi();
           LoadThucPham();
           LoadNhapKho();
           LoadDonViTinh();

        }

        public void LoadNhapKho()
        {
            DataTable dt = new DataTable();
            busNhapKho nkbus = new busNhapKho();
            dt = nkbus.LoadNhapKho();
            dgvNhapKho.DataSource = dt;
        }


        public void LoadNhaPhanPhoi()
        {
            DataTable dt = new DataTable();
            busNhaCungCap NCCBUS = new busNhaCungCap();
            dt = NCCBUS.LoadNhaCungCap();
            cbbNhaCungCap.DataSource = dt;
            cbbNhaCungCap.ValueMember = "Ma_NCC";
            cbbNhaCungCap.DisplayMember = "Ten_NCC";
        }

        public void LoadThucPham()
        {
            DataTable dt = new DataTable();
            busThucPham TPBUS = new busThucPham();
            dt = TPBUS.LoadThucPham();
            cbbThucPham.DataSource = dt;
            cbbThucPham.ValueMember = "Ma_TP";
            cbbThucPham.DisplayMember = "Ten_TP";
        }


        public void LoadGiaTheoMa()
        {
            int Ma = Convert.ToInt32( cbbThucPham.SelectedValue.ToString());
            busThucPham busthucpham = new busThucPham();
            dtoThucPham DTOTHUCPHAM = new dtoThucPham();
            DTOTHUCPHAM.Ma_TP = Ma;
            DataTable dt = new DataTable();
            dt = busthucpham.LoadGiaTheoMa(DTOTHUCPHAM);
            txtDonGia.Text = dt.Rows[0].ItemArray[0].ToString();


        }
        public void LoadDonViTinh()
        {
            DataTable dt = new DataTable();
            busDonViTinh TPBUS = new busDonViTinh();
            dt = TPBUS.LoadDonViTinh();
            cbbDonVi.DataSource = dt;
            cbbDonVi.ValueMember = "Ma_DV";
            cbbDonVi.DisplayMember = "Ten_DV";
        }

        private void cmbThucPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                LoadGiaTheoMa();
            }
            catch
            {

            }
        }
        //code tinh tong tien bang so luong * don gia
        public int TongTien()
        {
            int tongtien = 0;
            return tongtien;
        }
        private void btnNhapKho_Click(object sender, EventArgs e)
        {
            dtoNhapKho nhapkho = new dtoNhapKho();
            dtoCTNhapKho ctnhapkho = new dtoCTNhapKho();
            busNhapKho busNhapKho = new busNhapKho();
            busCTNhapKho busCTNhapKho = new busCTNhapKho();

            //them vao bang nhap kho
            nhapkho.NgayNhap = System.DateTime.Now.Date;
            nhapkho.TongTien = TongTien();
            nhapkho.Ma_NhapKho = busNhapKho.NhapKho(nhapkho);

             //them vao bang chi tiet nhap kho
            ctnhapkho.Ma_NhapKho = Convert.ToInt32(nhapkho.Ma_NhapKho);
            ctnhapkho.Ma_TP = Convert.ToInt32(cbbThucPham.SelectedValue);
            ctnhapkho.SoLuong = Convert.ToInt32(txtSoLuong.Text);
            ctnhapkho.GiaNhap = Convert.ToInt32(txtDonGia.Text);
            ctnhapkho.ThanhTien = ctnhapkho.SoLuong * ctnhapkho.GiaNhap;
            ctnhapkho.GhiChu = txtGhiChu.Text;

            busCTNhapKho.AddCTNhapKho(ctnhapkho);

            dgvNhapKho.DataSource = busNhapKho.LoadNhapKho();
        }
        private void btnHuy_Click(object sender, EventArgs e)
        {
           
        }


    }
}
